package com.mvu.banana.common.widget;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.ListBox;
import com.mvu.banana.common.client.CommonConstants;

/**
 * Class description...
 *
 * @author mvu
 */
public class EnumBox<T extends Enum> extends ListBox implements HasValue<T>{
  private Class<T> enumClass;

  public EnumBox(Class<T> enumClass) {
    this.enumClass = enumClass;
    for(T e : enumClass.getEnumConstants()){
      addItem(CommonConstants.get.getString(e.name()), e.name());
    };
  }

  @Override
  public T getValue() {
    return (T) T.valueOf(enumClass, getValue(getSelectedIndex()));
  }

  @Override
  public void setValue(T value) {
    setSelectedIndex(value.ordinal() + 1);
  }

  @Override
  public void setValue(T value, boolean fireEvents) {
    setValue(value);
    if(fireEvents){
      ValueChangeEvent.fire(this, value);
    }
  }

  @Override
  public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> tValueChangeHandler) {
    return null;  // TODO: implement me
  }
}
